package ua.maxicash.util.criteria;

import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import ua.maxicash.util.Filter;
import ua.maxicash.util.JqgridQuery;
import ua.maxicash.util.criteria.handlers.*;
import ua.maxicash.vo.Work;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class WorkCriteriaChain implements CriteriaChain {

    private List<CriteriaHandler> handlers;
    protected List<String> aliases = new ArrayList<String>();
    private DetachedCriteria criteria = DetachedCriteria.forClass(Work.class);
    private Junction junction = null;

    public WorkCriteriaChain(){
    }

    public void setUp(JqgridQuery jqgridQuery) {

        handlers = new LinkedList<CriteriaHandler>();

        if (jqgridQuery.getGroupOp().equals("AND")) {
            junction = Restrictions.conjunction();
            //criteria.add(Restrictions.conjunction());
        }

        if (jqgridQuery.getGroupOp().equals("OR")) {
            junction = Restrictions.disjunction();
            //criteria.add(Restrictions.disjunction());
        }

        CriteriaHandler ch = null;

        for (Filter filter : jqgridQuery.getRules()) {

            if (filter.getField().equals("city")) {
                ch = new CityCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("jobType")) {
                ch = new JobTypeCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("region")) {
                ch = new RegionCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("country")) {
                ch = new CountryCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("workDate")) {
                ch = new WorkDateCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("startTime")) {
                ch = new StartTimeCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("endTime")) {
                ch = new EndTimeCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("wage")) {
                ch = new WageCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

            if (filter.getField().equals("employer")) {
                ch = new EmployerCriteriaHandler(aliases);
                ch.setUp(filter);
                handlers.add(ch);
            }

        }


    }

    public DetachedCriteria executeChain() {

        for (CriteriaHandler handler : handlers) {
            handler.handle(criteria, junction);
        }

        criteria.add(junction);

        return criteria;

    }

}
